/* BeerScreen, Copyright (C) 2009  Gabriel Tremblay <initnull@gmail.com>
 * http://www.etrange.ca/beerscreen
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ca.etrange.beerscreen.model;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FilenameFilter;

import javax.swing.Timer;

/**
 * Monitor for file change with this simple algorithm :
 * - Check if file <index>-name.xml exist
 * - if yes
 * -- calculate checksum and check if file has changed
 * -- if yes
 * --- Tell the beer model to reload his informations
 * -- else
 * --- do nothing
 * - else
 * -- show empty beer 
 * @author init_null
 *
 */

public class BeerFileMonitor implements ActionListener {
	private static final String BEERS_DIR = "beers/";
	private final Timer updateTimer;
	private final FilenameFilter filter;
	private final Beer beer;
	private final File directory;
	private long lastTimestamp = 0;
	
	public BeerFileMonitor(int beerIndex, Beer beer){
		// hard coupling with beer
		this.beer = beer;
		
		// set up a filter for files, we only want .xml and file starting with <index>-
		final String fileBegin = beerIndex + "-";
		
		filter = new FilenameFilter() {
	        public boolean accept(File dir, String name) {
	            return name.endsWith(".xml") && name.startsWith(fileBegin);
	        }
	    };
	    
	    // Bind directory
	    directory = new File(BEERS_DIR);
	    
		// run the actionlistener once at startup (so it wont appear to lag) // hack
		actionPerformed(null);
		
		// set up actionListener
		updateTimer = new Timer(2000, this);
		updateTimer.start();

	}

	@Override
	/* Checksum all files to see if they changed. if yes, notify. */
	public void actionPerformed(ActionEvent e) {
		// We need to list directory each time since the name of the beer will change.
		String[] children = directory.list(filter);
		Long timestamp;
		File beerFile;

		if(children.length >= 1){			
			// calculate checksum
			beerFile = new File(BEERS_DIR + children[0]);
			timestamp = beerFile.lastModified();
			
			if(timestamp != lastTimestamp){
				// file modified
				lastTimestamp = timestamp;
				
				synchronized (beer) {
					beer.updateBeerInfoCallback(BEERS_DIR + children[0]);
				}
			}
		} else {
			// file does not exist
			beer.setBeerPresent(false);
			lastTimestamp = -1;
		}
	}
}
